<template>
  <view class="page">
    <!-- #ifndef H5-DINGDING -->
    <theme-navbar
      :is-white="false" :title="navTitle" :custom-back="goBack"
      style="z-index: 99999"
    ></theme-navbar>
    <!-- #endif -->
    <view></view>
    <view class="content">
      <u-form
        ref="uForm" v-model="formData" label-position="top"
        label-width="auto"
      >
        <u-form-item label="案件编号">
          <u-input
            placeHolder="" type="text" :disabled="true"
            v-model="formData.bizCode"
          ></u-input>
        </u-form-item>
        <u-form-item label="申请单位">
          <u-input
            placeHolder="" type="text" :disabled="true"
            v-model="formData.applyOrgName"
          ></u-input>
        </u-form-item>
        <u-form-item label="申请人">
          <u-input
            placeHolder="" type="text" :disabled="true"
            v-model="formData.applyPersonName"
          ></u-input>
        </u-form-item>
        <u-form-item label="申请日期">
          <u-input
            placeHolder="" type="text" :disabled="true"
            v-model="formData.applyTime"
          ></u-input>
        </u-form-item>
        <u-form-item label="申请原因">
          <u-input
            placeHolder="" type="text" :disabled="true"
            v-model="formData.applyReason"
          ></u-input>
        </u-form-item>
        <u-form-item label="审核人">
          <u-input
            placeHolder="" type="text" :disabled="true"
            v-model="formData.approvePersonName"
          ></u-input>
        </u-form-item>
        <u-form-item label="审核意见" prop="remark" required>
          <u-input
            placeHolder="" type="textarea" :disabled="readonly"
            v-model="formData.remark"
          ></u-input>
        </u-form-item>
        <u-form-item
          label="查阅期限" prop="startDate" ref="startChangeRef"
          required
        >
          <view style="display: flex; flex-direction: row; align-items: center; width: 100%">
            <u-input
              v-model="formData.startDate" :disabled="readonly" @click="ifShowStart=!ifShowStart&&!readonly"
              style="flex: 1" placeholder="请选择日期"
            />
            <view style="margin-left: 10rpx; margin-right: 10rpx; flex-shrink: 0">
              至
            </view>
            <u-input
              v-model="formData.endDate" :disabled="readonly" @click="ifShowEnd=!ifShowEnd&&!readonly"
              style="flex: 1" placeholder="请选择日期"
            />
          </view>
        </u-form-item>
      </u-form>
    </view>
    <view class="submit-btns btn-other-cls" v-if="!readonly">
      <u-button class="u-btn" type="primary" @click="handleConfrim('02')">
        同意
      </u-button>
      <u-button class="u-btn" type="primary" @click="handleConfrim('01')">
        不同意
      </u-button>
    </view>
    <!-- 时间选择器 -->
    <u-calendar
      mode="range" max-date="2050-01-01" v-model="ifShowStart"
      @change="changeDate('startDate',$event)"
    ></u-calendar>
    <u-calendar
      mode="range" max-date="2050-01-01" v-model="ifShowEnd"
      @change="changeDate('endDate',$event)"
    ></u-calendar>
  </view>
</template>
<script>
import DicChoice from '@/components/dic_choice.vue';
import DicInput from '@/components/dic_input.vue';
import commonService from '@/service/common/common.service';
import checkService from '@/service/check/check.service.js';

export default {
  components: {
    DicChoice,
    DicInput,
  },
  data() {
    return {
      formData: {
        applyId: '',
        approveId: '',
        isAgree: '',
        bizCode: '',
        fileNo: '',
        applyOrgName: '',
        applyPersonName: '',
        applyTime: '',
        applyReason: '',
        approvePersonName: '',
        remark: '',
        startDate: '',
        endDate: '',
        bizType: '',
      },
      prevParam: {},
      ifShowStart: false,
      ifShowEnd: false,
      readonly: true,
      bizType: 901003,
      rules: {
        remark: [
          {
            required: true,
            message: '请输入审核意见',
            // 可以单个或者同时写两个触发验证方式
            trigger: ['change', 'blur',],
          },
        ],
        startDate: [
          {
            required: true,
            message: '请选择查阅期限',
            // 可以单个或者同时写两个触发验证方式
            trigger: ['change', 'blur',],
          },
        ],
      },
    };
  },
  onLoad() {
    let opt = this.getRouteParam() || {};
    this.prevParam = opt;
    this.getBizTaskConfig();
  },
  onReady() {
    console.log(this.$refs.uForm, 11111);
    this.$refs.uForm.setRules(this.rules);
  },
  methods: {
    getBizTaskConfig() {
      this.$modal.showLoading('加载中...');
      commonService
        .getBizTaskConfigBybizCode({
          taskId: this.prevParam.taskUuid,
          bizCode: this.prevParam.bizCode,
          bizUuid: this.prevParam.bizUuid,
          processDefKey: this.prevParam.processDefKey,
          operateType: this.prevParam.operateType,
          sysCode: this.prevParam.sysCode,
          menuCode: '',
        })
        .then((res) => {
          if (res.success) {
            this.initList();
          } else {
            this.$u.alert(res.msg);
          }
        })
        .catch((e) => {
          this.$modal.hideLoading();
          this.$u.alert(res.msg);
        });
    },

    initList() {
      // 获取文书信息
      commonService
        .getArchivesApproveList({
          menuCode: '',
          applyId: this.prevParam.bizUuid,
          page: 1,
          limit: 15,
          bizType: this.bizType,
        })
        .then((res) => {
          this.$modal.hideLoading();
          if (res.success) {
            for (let key in this.formData) {
              this.formData[key] = res.data[0][key];
            }
            this.formData.remark = '同意';
            this.getFlowIsFinish();
          } else {
            this.$u.alert(res.msg);
          }
        })
        .catch((e) => {
          this.$modal.hideLoading();
        });
    },
    getFlowIsFinish() {
      // 判断是否为最后一步
      checkService
        .getFlowIsFinishDo({
          bizUuid: this.prevParam.bizUuid,
          noCheckPersonNodes: this.prevParam.processDefKey,
        })
        .then((res) => {
          if (res.success) {
            if (res.bean) {
              this.readonly = false;
            } else {
              this.readonly = true;
            }
            this.getFlowNodeStep();
          } else {
            this.$u.toast(res.msg);
          }
        });
    },
    // 获取审批节点信息
    getFlowNodeStep() {
      checkService
        .getFlowNodeStep({
          menuCode: '',
          bizUuid: this.prevParam.bizUuid,
          processDefKey: this.prevParam.processDefKey,
        })
        .then((res) => {
          if (res.success && res.bean.step == 'view') {
            this.readonly = true;
          } else {
            this.readonly = false;
          }
        })
        .catch((err) => {});
    },

    // 日期选择
    changeDate(type, e) {
      if (type == 'startDate') {
        this.formData.startDate = e.startDate;
      } else if (type == 'endDate') {
        this.formData.endDate = e.endDate;
      }
    },
    handleConfrim(key) {
      // if (!this.formData.remark) {
      //   return this.$u.toast('请输入审核意见!');
      // }
      // if (!this.formData.startDate) {
      //   return this.$u.toast('请选择查阅开始日期!');
      // }
      // if (!this.formData.endDate) {
      //   return this.$u.toast('请选择查阅结束日期!');
      // }
      this.$refs.uForm.validate((valid) => {
        if (valid) {
          // let starttime = new Date(this.formData.startDate);
          // let endtime = new Date(this.formData.endDate);
          // if (Date.parse(starttime) > Date.parse(endtime)) {
          //   return this.$u.toast('查阅开始日期不能小于查阅结束日期!');
          // }
          this.formData.isAgree = key;
          this.$modal.showLoading('加载中...');
          commonService
            .saveArchives(this.formData)
            .then((res) => {
              this.$modal.hideLoading();
              if (res.success) {
                this.backPrev({
                  paramKey: 'refresh',
                  param: true,
                });
              } else {
                this.$u.toast(res.msg);
              }
            })
            .catch((err) => {
              this.$modal.hideLoading();
              this.$u.toast(err.msg);
            });
        }
      });
    },
  },
};
</script>
<style scoped lang="scss">
	.page {
		padding: 0;
	}

	.sub-title {
		background: #f5f5f5;
		color: #989ea1;
		font-size: 24rpx;
		border-radius: 4px;
		margin-right: 10px;
		padding: 4px 8px;
	}

	.f-ctn {
		width: 100%;
	}

	.tb-lay {
		width: 900px;
		margin-left: 1px;
		margin-right: 1px;
	}

	.content {
		padding-left: $cm-content-paddng-row;
		padding-right: $cm-content-paddng-row;
		padding-top: $cm-conent-padding-column;
		padding-bottom: 70px;
	}

	/deep/.u-td {
		height: auto;
	}
</style>
